% using 1995 spillover for 1996~2001

clc;
clear all;
close all;

load addEstiProbRevised;
[row, col] = size(addEstiProbRevised);
Newmatrix = addEstiProbRevised;
Newmatrix(:, (col+1): (col+45)) = 0;

col_year = 1;
col_part = 41;
col_facilityID = 48;


col_estiprob = 71;    
col_lagcompanyinduspart1995 = 67;
col_lagratio_industry1995 = 65;

for i = 1: row
    year = Newmatrix(i, col_year);
    part = Newmatrix(i, col_part);  % part = 1, if facility participates
    estiprob = Newmatrix(i, col_estiprob);
    lagcompanyinduspart1995 = Newmatrix(i, col_lagcompanyinduspart1995);  % companyinduspart = 1, if its company has at least one participating facilty in the same industry
    lagindustryratio1995 = Newmatrix(i, col_lagratio_industry1995);
  %%%%%%% for regression P  (d.P in first-differenced eqn)  %%%%%%%%%
    % intra-firm within-industry spillover
    Newmatrix(i, col+1) = (1-part)* lagcompanyinduspart1995;
    Newmatrix(i, col+2) = (1-estiprob)* lagcompanyinduspart1995;
    
    % inter-frim spillover (within industry)
    Newmatrix(i, col+3) = (1-part)* (1-lagcompanyinduspart1995)*lagindustryratio1995;
    Newmatrix(i, col+4) = (1-estiprob)* (1-lagcompanyinduspart1995)*lagindustryratio1995;
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % interation term of tpost
  % tpost
  if (year>=1997)  
      tpost = 1;
  else
      tpost = 0;
  end
  Newmatrix(i, col+5) = tpost;
  Newmatrix(i, (col+6):(col+9)) = Newmatrix(i, (col+1):(col+4))* (1-tpost);
  Newmatrix(i, (col+10):(col+13)) = Newmatrix(i, (col+1):(col+4))* tpost;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% for regression length P  (i.e., P in first-differenced eqn)  %%%%%%%%%
   % part2 & estipart2
   if (year<=1995)  
    Newmatrix(i, col+14) = Newmatrix(i, col_part);  % part2
    Newmatrix(i, col+15) = Newmatrix(i, col_estiprob);  % estipart2
   end

   % intra-firm spillover
    % spillover when program exists
    Newmatrix(i, col+16) = Newmatrix(i, col+1)* (1-tpost);  
    Newmatrix(i, col+17) = Newmatrix(i, col+2)* (1-tpost);
    % spillover post-program
    Newmatrix(i, col+18) = Newmatrix(i, col+1)* tpost;
    Newmatrix(i, col+19) = Newmatrix(i, col+2)* tpost;
     
   
      
     
      
   % inter-firm spillover (coefficients not year-specific)
   % spillover when program exists
    Newmatrix(i, col+20) = Newmatrix(i, col+3)* (1-tpost);    
    Newmatrix(i, col+21) = Newmatrix(i, col+4)* (1-tpost);  
   % spillover post-program
    Newmatrix(i, col+22) = Newmatrix(i, col+3)* tpost;
    Newmatrix(i, col+23) = Newmatrix(i, col+4)* tpost;
   
   
   
   
   % inter-firm spillover (coefficients year-specific)
   Newmatrix(i, col+24+2*(year-1991)) = Newmatrix(i, col+3);
   Newmatrix(i, col+25+2*(year-1991)) = Newmatrix(i, col+4);
   
end




    
addYearSpecificSpillover = Newmatrix;

save addYearSpecificSpillover.mat addYearSpecificSpillover;
xlswrite('addYearSpecificSpillover',addYearSpecificSpillover);     
